package cn.itsource.org.controller;

import cn.itsource.base.util.AjaxResult;
import cn.itsource.base.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController    // @Controller  + @ResponseBody
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    /*传统写法*/
    /*@RequestMapping("/findall")
    @ResponseBody
    public List<Employee> findAll(){
        return employeeService.findAll();
    }*/

    /*restful风格*/
    /*查询所有*/
    @GetMapping
    @ApiOperation(value = "员工查询",notes = "查询所有")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /*查询一个*/
    @GetMapping("/{id}")
    @ApiOperation(value = "员工查询",notes = "根据ID查询单个")
    public Employee findOne(@PathVariable("id") Long id){
        return employeeService.findOne(id);
    }


    /*删除操作   localhost:8080/employee/1   @PathVariable 从资源路劲，提取参数
    * AjaxResult  封装数据{}
    * */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "员工删除",notes = "根据ID删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try{
            employeeService.remove(id);
            return AjaxResult.me().setResultObj("fasdfasdf");
        }catch (Exception e){
            e.printStackTrace();//打印异常信息【如果你动用的try-cathc 不写e.printStackTrace()】
            return AjaxResult.me().setMsg("删除失败");
        }

    }
    /*批量删除*/
    @PatchMapping
    @ApiOperation(value = "员工删除",notes = "根据ID删除")
    public AjaxResult patch(@RequestBody List<Long> ids){
        try{
            employeeService.patch(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();//打印异常信息【如果你动用的try-cathc 不写e.printStackTrace()】
            return AjaxResult.me().setMsg("删除失败");
        }

    }


    /*添加
    * sn,name,dirPath,state,manager_id,parent_id
    * 提交json格式的数据{sn:"xx",name:"dd",dirPath:"yy",state:1,manager:{id:1,username:"xxx"},parent:{}}
    *
    * 对象---json【后端----前端】
    * json---对象【前端----后端】
    *
    * mapper
    *       manager.id   parent.id
    * */
    @PutMapping
    @ApiOperation(value = "员工添加",notes = "员工添加")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        System.out.println(employee);
        try{
            if(employee.getId()==null){
                employeeService.add(employee);
            }else{
                employeeService.update(employee);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();//打印异常信息【如果你动用的try-cathc 不写e.printStackTrace()】
            return AjaxResult.me().setMsg("添加失败");
        }

    }
    /*分页接口{totalCount:"",data:[{},{}]}*/
    @PostMapping
    @ApiOperation(value = "员工分页查询",notes = "根据分页条件，查询")
    public PageList<Employee> queryForPage(@RequestBody EmployeeQuery query){

        PageList<Employee> pageList = employeeService.queryForPage(query);
        return pageList;
    }
}
